﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class NNHall : Game {
    public GameObject Root;
    EginUser user = EginUser.Instance;
    EginDN dn = EginDN.Instance;
    public UILabel UserName;
    public UILabel UID;
    public UILabel CoinCount;
    public UISprite avatarSprit;
    public GameObject ReturnBtn;
    private void Awake()
    {
        UIEventListener.Get(ReturnBtn).onClick = ButtonClick;
        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;
    }
    private void Start()
    {
        UserName.text = user.nickname;
        UID.text = user.Uid;
        string str = user.ReturnRelCoin(int.Parse(user.coinCount));
        CoinCount.text = str;
        avatarSprit.spriteName = user.avatarId.ToString();
        if (user.ControlScene == 1)
        {

            //牛牛
            GameObject FriendRoomPanel = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameDNPrafabPath.FriendRoomPanel)));
        }
        else {

            //牛牛
            GameObject CoinRoom = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameDNPrafabPath.CoinRoom)));
        }
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "ReturnBtn")
        {
            Application.LoadLevel(ScenePath.Hall);
        }
        
    }
    /// <summary>
    /// 监听服务器消息
    /// </summary>
    /// <param name="data"></param>
    public override void SocketReceiveMessage(byte[] data)
    {
        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(4).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);
        //牛牛创建房间返回
        if (TempMsgID == MsgId.NnOpenRoomResponse)
        {
            Pb.NnOpenRoomResponse newpb = Pb.NnOpenRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                dn.createrUid = newpb.RoomInfo.CreaterUid;
                dn.roomId = newpb.RoomInfo.RoomId;
                dn.totalRound = newpb.RoomInfo.TotalRound;
                dn.seatId = newpb.SeatId;
                dn.roomStatus = newpb.RoomInfo.RoomStatus;
                Debug.Log(dn.seatId + "hall");
                user.gameUserInfoList .Add(newpb.UserInfo);
                Application.LoadLevelAsync(ScenePath.GameDNPlay);
            }
            else {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //牛牛根据ID加入房间返回
        else if (TempMsgID == MsgId.NnEnterRoomResponse)
        {
            Pb.NnEnterRoomResponse newpb = Pb.NnEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                dn.createrUid = newpb.RoomInfo.CreaterUid;
                dn.roomId = newpb.RoomInfo.RoomId;
                dn.totalRound = newpb.RoomInfo.TotalRound;
                dn.seatId = newpb.SeatId;
                dn.roomStatus = newpb.RoomInfo.RoomStatus;
                user.gameUserInfoList = newpb.UserInfoList.ToList();
                Application.LoadLevelAsync(ScenePath.GameDNPlay);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //牛牛随机进入房间返回
        else if (TempMsgID == MsgId.NnRandomEnterRoomResponse)
        {
            Pb.NnRandomEnterRoomResponse newpb = Pb.NnRandomEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                dn.createrUid = newpb.RoomInfo.CreaterUid;
                dn.roomId = newpb.RoomInfo.RoomId;
                dn.totalRound = newpb.RoomInfo.TotalRound;
                dn.seatId = newpb.SeatId;
                dn.roomStatus = newpb.RoomInfo.RoomStatus;
                user.gameUserInfoList = newpb.UserInfoList.ToList();
                Application.LoadLevelAsync(ScenePath.GameDNPlay);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }

        }
        //网络异常
        else if (TempMsgID == MsgId.NetWorkAbnormalMessage)
        {
            GameObject NetWorkAbnormal = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("NetWorkAbnormal")));
        }
    }


    
}
